

EFFECT.Mat = Material( "effects/tool_tracer" )
EFFECT.MatStart = Material( "Sprites/light_glow02_add" )
EFFECT.MatEnd = Material( "Sprites/light_glow02_add_noz" )

/*---------------------------------------------------------
   Init( data table )
---------------------------------------------------------*/
function EFFECT:Init( data )
	self.Player = data:GetEntity()
	self.Attachment = data:GetAttachment()
	self.Weapon = self.Player:GetActiveWeapon():GetClass()
	local col = data:GetAngles()
	self.Colour = Color(col.p, col.y, col.r)
	
	printd( "Healtracer init ", self.Player, "CLIENT:", CLIENT )
	
	self.AttachOffset = 2
end
function EFFECT:TargetPos( start )
	if ( !IsValid( self.Player ) ) then return self.EndPos end
	local shoot, aim = self.Player:GetShootPos(), self.Player:GetAimVector()
	local target = util.QuickTrace( shoot, aim * 192, self.Player )
	local trace = util.TraceLine( { start = start, endpos = target.HitPos, filter = self.Player } )
	if ( !trace.StartSolid ) then
		return trace.HitPos
	else
		local newtrace = util.QuickTrace( shoot - ( aim * 16 ), aim * 18, self.Player )
		return newtrace.HitPos + ( aim * 14 )
	end
end
function EFFECT:AttachmentPosition( attachment, offset )
	local vm = self.Player:GetActiveWeapon()
	if ( !IsValid( vm ) ) then return end
	if ( self.Player == LocalPlayer() ) then
		vm = LocalPlayer():GetViewModel()
	end
	
	local attach = vm:GetAttachment( attachment )
	if ( !attach ) then return end
	
	return attach.Pos - ( attach.Ang:Up() * offset )
end

/*---------------------------------------------------------
   THINK
---------------------------------------------------------*/
function EFFECT:Think( )
	self.StartPos = self:AttachmentPosition( self.Attachment, self.AttachOffset )
	self.EndPos = self:TargetPos( self:AttachmentPosition( self.Attachment, self.AttachOffset ) )
	
	if ( !self.StartPos || !self.EndPos ) then return true end
	self.Entity:SetRenderBoundsWS( self.StartPos, self.EndPos )
	
	if ( !IsValid( self.Player ) ) then return false end
	local weap = self.Player:GetActiveWeapon()
	if ( !IsValid( weap ) || weap:GetClass() != self.Weapon ) then return false end
	if ( !weap:GetNWBool( "on" ) ) then return false end

	return true

end

/*---------------------------------------------------------
   Draw the effect
---------------------------------------------------------*/
function EFFECT:Render( )
	if ( !self.StartPos || !self.EndPos ) then return end
	
	render.SetMaterial(self.MatStart)
	render.DrawSprite( self.StartPos, 16, 16, self.Colour )
	
	if ( self.Player == LocalPlayer() ) then
		render.SetMaterial(self.MatEnd)
	end
	render.DrawSprite( self.EndPos, 16 + ( math.random() * 4 ), 16 + ( math.random() * 4 ), self.Colour )
	
	self.Length = (self.StartPos - self.EndPos):Length()
		
	render.SetMaterial( self.Mat )
	local texcoord = math.Rand (0, 1)
	
	for i=1, 4 do
		render.DrawBeam(self.StartPos, 										// Start
					self.EndPos,											// End
					 8,													// Width
					 texcoord,														// Start tex coord
					 texcoord + self.Length / 128,									// End tex coord
					 self.Colour)		// Color (optional)
	end

end
